//
//  StatusViewController+MenuDelegate.m
//  TwitterPlane
//
//  Created by kazuyuki takahashi on 10/04/14.
//  Copyright 2010 by invisibledesigner.com. All rights reserved.
//

#import "StatusViewController+MenuDelegate.h"
#import "Status.h"
#import "NSUserDefaults+App.h"
#import "TwitterEngineManager.h"

//menu item tags
static const NSInteger kOpenTag = 1;
static const NSInteger kReplyTag = 2;
static const NSInteger kDeleteTag = 3;


@implementation StatusViewController(MenuDelegate)

- (void)menuWillOpen:(NSMenu*)menu
{
    Status *s = (Status*)[self representedObject];
    
    NSMenuItem *reply = [menu itemWithTag:kReplyTag];
    NSAssert(reply, @"set kReplyTag with IB");
    NSString *replyTitle = [@"Reply to " stringByAppendingString:[[s user] screenName]];
    [reply setTitle:replyTitle];
    
    NSMenuItem *del = [menu itemWithTag:kDeleteTag];
    NSAssert(del, @"set kDeleteTag with IB");
    NSString *myname = [User myScreenName];
    BOOL isDeletable = [myname isEqualToString:[[s user] screenName]];  //判定方法あやしい
    [del setEnabled:isDeletable];
}

- (IBAction)openUserPage:(id)sender
{
    Status *s = (Status*)[self representedObject];
    [[NSWorkspace sharedWorkspace] openURL:[[s user] profileURL]];
}

- (IBAction)deleteStatus:(id)sender
{
    TwitterEngineCallback callback = ^(BOOL succeeded, NSError *error) {
        if (!succeeded) {
            [[NSAlert alertWithError:error] runModal];
        }
    };

    [[TwitterEngineManager defaultManager] deleteStatus:[self representedObject]
                                               callback:callback];
}

@end
